package com.innovations.inn.service.impl;

import com.innovations.common.utils.DateUtils;
import com.innovations.common.utils.uuid.IdGeneratorSnowflake;
import com.innovations.inn.domain.BusinessIndividual;
import com.innovations.inn.domain.ColumnInformation;
import com.innovations.inn.domain.ContributeInformation;
import com.innovations.inn.domain.Enterprise;
import com.innovations.inn.domain.ResearchInstitution;
import com.innovations.inn.mapper.ContributeInformationMapper;
import com.innovations.inn.service.IBusinessIndividualService;
import com.innovations.inn.service.IColumnInformationService;
import com.innovations.inn.service.IContributeInformationService;
import com.innovations.inn.service.IEnterpriseService;
import com.innovations.inn.service.IResearchInstitutionService;
import com.innovations.system.service.impl.SysUserServiceImpl;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import static com.innovations.common.utils.MyBeanUtils.copyProperties;

/**
 * 投稿栏目信息Service业务层处理
 *
 * @author dp
 * @date 2024-11-26
 */
@Service
public class ContributeInformationServiceImpl implements IContributeInformationService {
    @Autowired
    ContributeInformationMapper contributeInformationMapper;
    @Autowired
    IdGeneratorSnowflake idGeneratorSnowflake;
    @Autowired
    IResearchInstitutionService researchInstitutionService;
    @Autowired
    IBusinessIndividualService businessIndividualService;
    @Autowired
    IEnterpriseService enterpriseService;
    @Autowired
    SysUserServiceImpl sysUserService;
    @Autowired
    IColumnInformationService columnInformationService;

    /**
     * 查询投稿栏目信息
     *
     * @param contributeInformationId 投稿栏目信息主键
     * @return 投稿栏目信息
     */
    @Override
    public ContributeInformation selectContributeInformationByContributeInformationId(Long contributeInformationId) {
        return contributeInformationMapper.selectContributeInformationByContributeInformationId(contributeInformationId);
    }

    /**
     * 查询投稿栏目信息列表
     *
     * @param contributeInformation 投稿栏目信息
     * @return 投稿栏目信息
     */
    @Override
    public List<ContributeInformation> selectContributeInformationList(ContributeInformation contributeInformation) {
        return contributeInformationMapper.selectContributeInformationList(contributeInformation);
    }

    /**
     * 新增投稿栏目信息
     *
     * @param contributeInformation 投稿栏目信息
     * @return 结果
     */
    @Override
    public int insertContributeInformation(ContributeInformation contributeInformation) {
        return contributeInformationMapper.insertContributeInformation(contributeInformation);
    }

    /**
     * 修改投稿栏目信息
     *
     * @param contributeInformation 投稿栏目信息
     * @return 结果
     */
    @Override
    public int updateContributeInformation(ContributeInformation contributeInformation) {
        contributeInformation.setUpdateTime(DateUtils.getNowDate());
        // 审核通过
        if (contributeInformation.getStatus() == 3) {
            // 写入栏目信息
            ColumnInformation columnInformation = copyProperties(contributeInformation, ColumnInformation.class);
            columnInformation.setColumnInformationId(contributeInformation.getContributeInformationId());
            columnInformation.setColumnInformationTitle(contributeInformation.getContributeInformationTitle());
            columnInformation.setColumnInformationType(contributeInformation.getContributeInformationType());
            columnInformation.setColumnInformationDesc(contributeInformation.getContributeInformationDesc());
            columnInformation.setColumnInformationCover(contributeInformation.getContributeInformationCover());
            columnInformation.setColumnInformationContent(contributeInformation.getContributeInformationContent());
            columnInformation.setStatus(0L);
            ColumnInformation oldColumnInformation = columnInformationService.selectColumnInformationByColumnInformationId(columnInformation.getColumnInformationId());
            if (oldColumnInformation == null) {
                // 初始数据补全
                columnInformationService.completeData(columnInformation, columnInformation.getCreateBy(), 1);
                // 更新发布人、发布时间
                columnInformation.setReleaseBy(contributeInformation.getUpdateBy());
                columnInformation.setReleaseTime(DateUtils.getNowDate());
                // 保存栏目信息
                columnInformationService.insertColumnInformation(columnInformation);
            }
        }
        return contributeInformationMapper.updateContributeInformation(contributeInformation);
    }

    /**
     * 批量删除投稿栏目信息
     *
     * @param contributeInformationIds 需要删除的投稿栏目信息主键
     * @return 结果
     */
    @Override
    public int deleteContributeInformationByContributeInformationIds(Long[] contributeInformationIds) {
        return contributeInformationMapper.deleteContributeInformationByContributeInformationIds(contributeInformationIds);
    }

    /**
     * 删除投稿栏目信息信息
     *
     * @param contributeInformationId 投稿栏目信息主键
     * @return 结果
     */
    @Override
    public int deleteContributeInformationByContributeInformationId(Long contributeInformationId) {
        return contributeInformationMapper.deleteContributeInformationByContributeInformationId(contributeInformationId);
    }

    /**
     * 实体数据补全
     *
     * @param contributeInformation 实体
     * @param userId                用户id
     * @param crateType             创建类型 1：新增 2：修改
     */
    @Override
    public void completeData(ContributeInformation contributeInformation, Long userId, Integer crateType) {


        // 创建模式
        if (crateType == 1) {
            // 标识
            if (contributeInformation.getContributeInformationId() == null) {
                contributeInformation.setContributeInformationId(idGeneratorSnowflake.nextId());
            }
            // 创建人、创建时间
            if (contributeInformation.getCreateBy() == null) {
                contributeInformation.setCreateBy(userId);
            }
            if (contributeInformation.getCreateTime() == null) {
                contributeInformation.setCreateTime(DateUtils.getNowDate());
            }
            // 逻辑删除
            if (contributeInformation.getIsDeleted() == null) {
                contributeInformation.setIsDeleted(0L);
            }
            // 机构类型 1.科研机构 2.企业 3.商业用户
            if (contributeInformation.getOrgId() == null) {
                switch (contributeInformation.getOrgType()) {
                    case 1:
                        ResearchInstitution researchInstitution = new ResearchInstitution();
                        researchInstitution.setUserId(userId);
                        List<ResearchInstitution> researchInstitutionList = researchInstitutionService.selectResearchInstitutionList(researchInstitution);
                        if (researchInstitutionList == null || researchInstitutionList.isEmpty()) {
                            throw new RuntimeException("您当前的可以机构不存在，不允许添加");
                        }
                        contributeInformation.setOrgId(researchInstitutionList.get(0).getInstitutionId());
                        contributeInformation.setOrgName(researchInstitutionList.get(0).getInstitutionName());
                        break;
                    case 2:
                        Enterprise enterprise = new Enterprise();
                        enterprise.setUserId(userId);
                        List<Enterprise> enterpriseList = enterpriseService.selectEnterpriseList(enterprise);
                        if (enterpriseList == null || enterpriseList.isEmpty()) {
                            throw new RuntimeException("您当前的商业机构不存在，不允许添加");
                        }
                        contributeInformation.setOrgId(enterpriseList.get(0).getEnterpriseId());
                        contributeInformation.setOrgName(enterpriseList.get(0).getEnterpriseName());
                        break;
                    case 3:
                        BusinessIndividual businessIndividual = new BusinessIndividual();
                        businessIndividual.setUserId(userId);
                        List<BusinessIndividual> businessIndividualList = businessIndividualService.selectBusinessIndividualList(businessIndividual);
                        if (businessIndividualList == null || businessIndividualList.isEmpty()) {
                            throw new RuntimeException("您当前非商业用户，不允许添加");
                        }
                        contributeInformation.setOrgId(businessIndividualList.get(0).getBusinessIndividualId());
                        contributeInformation.setOrgName(sysUserService.selectUserById(userId).getNickName());
                        break;
                }
            }
        }
        // 修改模式
        else if (crateType == 2) {
            // 标识
            if (contributeInformation.getContributeInformationId() == null) {
                throw new RuntimeException("您当前修改的投稿栏目信息不存在");
            }
        }
    }

}
